# -*- coding: utf-8 -*-
"""
@Time    : 2021/3/18 13:20
@Author  : hui lin
"""


from client.application import Application
from client.views.base import SCREEN_REGISTER_CLASS, ScreenBase
from pygame import Surface
from typing import Optional, Callable, Type

from obespoir.share.espoir_log import global_log as logger

_need_reload = True


class SceneManager(object):

    def __init__(self):
        self.app: Optional[Application] = None
        self._screen: Optional[Surface] = None
        self._cur_scene_name: Optional[None, str] = None

    def set_screen(self, screen: Surface, app: Application):
        self._screen = screen
        self.app = app

    def switch_to_scene(self, scene_name, **kwargs):
        if self._screen is None:
            return

        if scene_name not in SCREEN_REGISTER_CLASS or \
                self._cur_scene_name == scene_name:
            return

        if self._cur_scene_name:
            self.app.del_scene_event(self._cur_scene_name)
        tar_cls: Callable[[Surface], Type[ScreenBase]] = \
            SCREEN_REGISTER_CLASS.get(scene_name)
        self._cur_scene_name = scene_name
        tar_obj = tar_cls(self._screen)
        self.app.add_scene_event(scene_name, tar_obj.update)
        tar_obj.init_screen(**kwargs)
        logger.info("success", scene_name=scene_name, kwargs=kwargs)


scene_mgr = SceneManager()
